﻿using Cirrious.MvvmCross.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace TealightV5.Models
{
    internal class ArticleFinder
    {
        private const string articleUrl = "https://dl.dropbox.com/u/1814002/test.txt";

        public static void StartAsyncSearch(string searchText, Action<IEnumerable<Article>> success, Action<Exception> error)
        {
            MvxAsyncDispatcher.BeginAsync(() => DoAsyncSearch(searchText, success, error));
        }

        private static void DoAsyncSearch(string searchText, Action<IEnumerable<Article>> success, Action<Exception> error)
        {
            var search = new ArticleFinder(searchText, success, error);
            search.StartSearch();
        }

        private readonly string _searchText;
        private readonly Action<IEnumerable<Article>> _success;
        private readonly Action<Exception> _error;

        public ArticleFinder(String searchText, Action<IEnumerable<Article>> success, Action<Exception> error)
        {
            this._searchText = searchText;
            this._success = success;
            this._error = error;
        }

        private void StartSearch()
        {
            try
            {
                // perform the search
                string uri = articleUrl;
                var request = WebRequest.Create(new Uri(uri));
                request.BeginGetResponse(ReadCallback, request);
            }
            catch (Exception exception)
            {
                _error(exception);
            }
        }

        private void ReadCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                var request = (HttpWebRequest)asynchronousResult.AsyncState;
                var response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

                ArticleCollection articles = new ArticleCollection();

                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader responseStreamReader = new StreamReader(responseStream))
                    {
                        String line;
                        while ((line = responseStreamReader.ReadLine()) != null)
                        {
                            if (_searchText == "*")
                            {
                                Article article = new Article() { ArticleType = Enumerators.ArticleType.News, Name = line, Title = line };
                                articles.Add(article);
                            }
                            else if (line.Contains(_searchText))
                            {
                                Article article = new Article() { ArticleType = Enumerators.ArticleType.News, Name = line, Title = line };
                                articles.Add(article);
                            }
                        }
                    }
                }

                _success(articles);
            }
            catch (Exception exception)
            {
                _error(exception);
            }
        }
    }
}